package presentacion;

import integracion.DatabaseManager;
import integracion.ComponenteDAO;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import share.Componente;


/**
 * Servlet implementation class for Servlet: Sesion
 *
 */
 public class ModificarComponente extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
 {
   static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doAll(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doAll(request, response);
	}

	private void doAll(HttpServletRequest request, HttpServletResponse response) throws IOException
	{

		if(request.getParameter("accion").equals("Modificar"))
		{
			Componente p = new Componente();
			p.setId(Integer.parseInt(request.getParameter("id")));
			p.setNombre(request.getParameter("nombre"));
			p.setDescripcion(request.getParameter("descripcion"));

			try
			{
				ComponenteDAO dao = new ComponenteDAO(DatabaseManager.getConnection());
				dao.modificar(p);
				response.sendRedirect("index.jsp?page=GestionarComponente/GestionarComponente");
				return;

			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}

		if(request.getParameter("accion").equals("datos"))
		{
			int id = Integer.parseInt(request.getParameter("opciones"));
			Componente p = new Componente();

			try
			{
				ComponenteDAO dao = new ComponenteDAO(DatabaseManager.getConnection());
				p= dao.getComponenteById(id);
				response.sendRedirect("index.jsp?page=GestionarComponente/formularioModificarComponente&id="+p.getId()+
						"&nombre="+p.getNombre()+"&descripcion="+p.getDescripcion());
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}


	}
}